<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
Class Campaign extends MY_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->data['controller'] = 'campaign';
        $this->load->model('Mcampaign');
        $this->load->model('Mindex');
		$this->load->model('Mcampaignuser');
    }
    
    public function place()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $id_place = $this->getParamInt('place_id');
        if(!in_array($id_place, array(null, '', '0'))) // khác null
        {
            $this->data['id_active'] = $this->getParamUri(3,3);
            $this->load->library('pagination');
            $config['per_page'] = $this->data['per_page'] =  10;
            $this->data['page'] = $this->getParamUri(3,3);
            $data = $this->Mcampaign->camPaignPlace($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'), $id_place);
            $this->data['dataPlaceInfo'] = $this->Mcampaign->dataPlaceInfo($id_place);
            //die('<pre>'.print_r($this->data['dataPlaceInfo']));
            $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
            $config['total_rows'] = $data['count'];
            $config['uri_segment'] = 3;
            $config['uri_segment_page'] = $this->getParamUri(3,3);
            $config['suffix'] = '.html';
			$config['paramhack'] = '?place_id='.$id_place;
            $this->pagination->initialize($config);
            $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
            $this->data['data'] = $data['data'];
            $this->data['action'] = 'campaignplace';
            $this->load->view('temp/layout', $this->data);   
        }else{
            $this->data['id_active'] = $this->getParamUri(3,3);
            $this->load->library('pagination');
            $config['per_page'] = $this->data['per_page'] =  10;
            $this->data['page'] = $this->getParamUri(3,3);
            $data = $this->Mcampaign->placeDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
            $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
            $config['total_rows'] = $data['count'];
            $config['uri_segment'] = 3;
            $config['uri_segment_page'] = $this->getParamUri(3,3);
            $config['suffix'] = '.html';
			
            $this->pagination->initialize($config);
            $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
            $this->data['data'] = $data['data'];
            $this->data['action'] = 'place';
            $this->load->view('temp/layout', $this->data); 
        }
    }

    public function pendding()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaign->getCampaignPenddingDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        $this->data['action'] = 'pendding';
        $this->load->view('temp/layout', $this->data);
    }
    
	public function all(){
	    $this->checkPermission($this->session->userdata('level'), array(1));
		$this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaign->getCampaignAllDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        $this->data['action'] = 'all';
        $this->load->view('temp/layout', $this->data);
	}
	
	
	public function stop(){
	    $this->checkPermission($this->session->userdata('level'), array(1));
		$this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaign->getCampaignStopDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        $this->data['action'] = 'stop';
        $this->load->view('temp/layout', $this->data);
	}
	public function cancel(){
		$this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaign->getCampaignCancelDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];
        $this->data['action'] = 'cancel';
        $this->load->view('temp/layout', $this->data);
	}
	
    public function running()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaign->getCampaignDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        //$this->data['static'] = $this->Mindex->getStatic($data['idCampaign']);
        
        $this->data['action'] = 'running';
        $this->load->view('temp/layout', $this->data);
    }
    
    private function redirectnullid()
    {
        redirect($this->data['baseurl'].'campaign/tat-ca');
    }
    
    public function viewcampaign()
    {
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->load->library('form_validation');
        
        $this->form_validation->set_rules('title', 'Tên chiến dịch', 'required');
        $this->form_validation->set_rules('time_start', 'Thời gian bắt đầu', 'required');
        $this->form_validation->set_rules('time_end', 'Thời gian kết thúc', 'required');
		$this->form_validation->set_rules('active', 'Trạng thái', 'required');
        $this->form_validation->set_rules('old_active','Trạng thái cũ', 'required' );
        $this->form_validation->set_rules('oneview','Kiểu hiển thị', 'required' );
		$id = $this->getParamInt('id');
        if(in_array($id, array(null, '', '0')))
        {
            $this->redirectnullid();
        }
		$this->data['cid'] = $id;
		$this->data['data'] = $this->Mcampaignuser->getCampaignuserDetail($id);
        $this->data['dataListShop'] = $this->Mcampaignuser->dataListShop();
		$this->data['getListActive'] = $this->Mcampaign->getListActive($this->data['data']['data']->active);
		//echo '<pre>';die(print_r($this->data['getListActive']));
		if($this->form_validation->run() === TRUE)
        {
            $title = $this->getParamString('title');
            $time_start = $this->getParamString('time_start');
            $time_end = $this->getParamString('time_end');
			$active = $this->getParamInt('active');
            $old_active = $this->getParamInt('old_active');
            $link_campaign = $this->getParamString('link_campaign');
            $folder_campaign = $this->getParamString('folder_campaign');
            $link_redirect = $this->getParamString('link_redirect');
            $oneview = $this->getParamInt('oneview');
            if( (int)$active == 2 ){
                if( in_array($link_campaign,array('',null)) || in_array($folder_campaign,array('',null)) || in_array($link_redirect,array('',null)) ){
                    die('<meta charset="utf-8"><script>alert("Điền thiếu thông tin");window.location.href="'.$this->data['baseurl'].'view-campaign?id='.$this->getParamInt('id').'";</script>');
                } 
            }
			$run = $this->Mcampaign->updateCampaign($title,$time_start,$time_end,$active,$old_active,$link_campaign,$folder_campaign,$this->getParamInt('id'),$oneview,$link_redirect);
			if($run == 200)
            {
                die('<meta charset="utf-8"><script>alert("Cập nhật thành công");window.location.href="'.$this->data['baseurl'].'view-campaign?id='.$this->getParamInt('id').'";</script>');
            }
        }
        $this->data['action'] = 'viewcampaign';
        $this->load->view('temp/layout', $this->data);
    }
	
	public function addplace(){
		$this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('shopName', 'Tên cửa hàng', 'required');
        $this->form_validation->set_rules('address', 'Địa chỉ', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->form_validation->set_rules('ip', 'Dải mạng IP', 'required');
        $this->form_validation->set_rules('lat', 'Kinh độ latitude', 'required');
        $this->form_validation->set_rules('long', 'Vĩ độ longitude', 'required');
		$this->form_validation->set_rules('nas_id','Modem','required');
		$this->data['getListModem'] = $this->Mcampaign->getListModem('');
        if ($this->form_validation->run() === TRUE)
        {
            $shopName = $this->getParamString('shopName');         
            $address = $this->getParamString('address'); 
			$mobile = $this->getParamString('mobile'); 
			$ip = $this->getParamString('ip'); 
			$lat = $this->getParamString('lat'); 
			$long = $this->getParamString('long'); 
			$nas_id = $this->getParamString('nas_id'); 
            $this->data['result'] = $this->Mcampaign->addPlace($shopName, $address, $mobile, $ip, $lat, $long, $nas_id);
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'campaign/dia-diem.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'addplace';
        $this->load->view('temp/layout', $this->data);
	}
	
	public function editplace(){
		$this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('shopName', 'Tên cửa hàng', 'required');
        $this->form_validation->set_rules('address', 'Địa chỉ', 'required');
        $this->form_validation->set_rules('mobile', 'Số điện thoại', 'required');
        $this->form_validation->set_rules('ip', 'Dải mạng IP', 'required');
        $this->form_validation->set_rules('lat', 'Kinh độ latitude', 'required');
        $this->form_validation->set_rules('long', 'Vĩ độ longitude', 'required');
		$this->form_validation->set_rules('nas_id','Modem','required');
		$id = $this->getParamUri(3,1);
		$this->data['id'] = $id;
		$this->data['getPlaceInfo'] = $this->Mcampaign->getPlaceInfo($id);
		$this->data['getListModem'] = $this->Mcampaign->getListModem($this->data['getPlaceInfo']->MaC);
        if ($this->form_validation->run() === TRUE)
        {
            $shopName = $this->getParamString('shopName');         
            $address = $this->getParamString('address'); 
			$mobile = $this->getParamString('mobile'); 
			$ip = $this->getParamString('ip'); 
			$lat = $this->getParamString('lat'); 
			$long = $this->getParamString('long'); 
			$nas_id = $this->getParamString('nas_id'); 
            $this->data['result'] = $this->Mcampaign->editPlace($shopName, $address, $mobile, $ip, $lat, $long, $nas_id, $id);
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thành công");window.location.href="'.$this->data['baseurl'].'campaign/sua-dia-diem/'.$id.'.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'editplace';
        $this->load->view('temp/layout', $this->data);
	}
    
    public function modem(){
        $this->checkPermission($this->session->userdata('level'), array(1));
		$this->data['id_active'] = $this->getParamUri(3,3);
        $this->load->library('pagination');
        $config['per_page'] = $this->data['per_page'] =  10;
        $this->data['page'] = $this->getParamUri(3,3);
        $data = $this->Mcampaign->getListModemDB($config['per_page'],(($this->getParamUri(3,3) - 1) * $config['per_page'] > 0 ? ($this->getParamUri(3,3) - 1) * $config['per_page'] : 0),$this->session->userdata('id_user'));
        $config['base_url'] = base_url($this->uri->segment(1).'/'.$this->uri->segment(2));//$this->uri->segment(2).'/');
        $config['total_rows'] = $data['count'];
        $config['uri_segment'] = 3;
        $config['uri_segment_page'] = $this->getParamUri(3,3);
        $config['suffix'] = '.html';
        $this->pagination->initialize($config);
 
        $this->data['pageg'] = $this->data['page'] - 1 > 0 ? $this->data['page'] - 1 : 0;
        $this->data['data'] = $data['data'];

        $this->data['action'] = 'modem';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function addmodem(){
        $this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên modem', 'required');
        $this->form_validation->set_rules('nas_id', 'NAS ID', 'required');
        if ($this->form_validation->run() === TRUE)
        {
            $name = $this->getParamString('name');         
            $nas_id = $this->getParamString('nas_id'); 
            $this->data['result'] = $this->Mcampaign->addModem($name,$nas_id);
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Thêm thành công");window.location.href="'.$this->data['baseurl'].'campaign/modem.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'addmodem';
        $this->load->view('temp/layout', $this->data);
    }
    
    public function editmodem(){
		$this->checkPermission($this->session->userdata('level'), array(1));
        $this->checkAdmin($this->session->userdata('id_user'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('name', 'Tên modem', 'required');
        $this->form_validation->set_rules('nas_id', 'NAS ID', 'required');
		$id = $this->getParamUri(3,1);
		$this->data['id'] = $id;
		$this->data['getModemInfo'] = $this->Mcampaign->getModemInfo($id);
        if ($this->form_validation->run() === TRUE)
        {
            $name = $this->getParamString('name');         
            $nas_id = $this->getParamString('nas_id'); 
            $this->data['result'] = $this->Mcampaign->editModem($name, $nas_id, $id);
            if($this->data['result'] == 200)
            {
                die('<meta charset="utf-8"><script>alert("Sửa thành công");window.location.href="'.$this->data['baseurl'].'campaign/sua-modem/'.$id.'.html";</script>');
            }else{
                $this->data['result'] = $this->data['result'];
            }
        }
        $this->data['action'] = 'editmodem';
        $this->load->view('temp/layout', $this->data);
	}
    
    
}